﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Expression;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

namespace Mang24k99.Model.SPECT
{
    [ActiveRecord(Table = "KT_SPECT_SUBTYPE")]
    public partial class MKtSpectSubtype:ActiveRecordBase
    {
        public static IList<MKtSpectSubtype> SelectAll()
        {
            return (IList<MKtSpectSubtype> )FindAll(typeof(MKtSpectSubtype));
        }
        public static MKtSpectSubtype SelectByPrimaryKey(object ID)
        {
            return (MKtSpectSubtype)FindByPrimaryKey(typeof(MKtSpectSubtype), ID);
        }

        public static IList<MKtSpectSubtype>  SelectALLByProperty(string property, object value)
        {
            return (IList<MKtSpectSubtype> )FindAllByProperty(typeof(MKtSpectSubtype), property, value);
        }
        public static IList<MKtSpectSubtype>  SelectALLSliced(int firstResult, int maxResult, NHibernate.Expression.DetachedCriteria criteria)
        {
        
            return (IList<MKtSpectSubtype> )SlicedFindAll(typeof(MKtSpectSubtype), firstResult, maxResult, criteria);
        }
        protected override void OnDelete()
        {
            MKtSpectSubtypeHistory mHistory = new MKtSpectSubtypeHistory();
            Common.ModelCommon.CopyObjectValues(this, mHistory);
            mHistory.DeleteTime = DateTime.Now;
            mHistory.Create();
            base.OnDelete();
        }
        /// <summary>
        /// 创建一条记录
        /// </summary>
        /// <param name="subtype"></param>
        public static void Create(MKtSpectSubtype subtype)
        {
            subtype.Create();
        }
        //删除一条记录
        public static void Delete(MKtSpectSubtype subtype)
        {
            subtype.Delete();
        }
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="subtype"></param>
        public static void Update(MKtSpectSubtype subtype)
        {
            subtype.Update();
        }
        /// <summary>
        /// 通过where语句返回符合条件记录
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public static IList<MKtSpectSubtype> SelectByWhere(string where)
        {
            string hql = @"from MKtSpectSubtype subtype where " + where;
            SimpleQuery query = new SimpleQuery(typeof(MKtSpectSubtype), hql);
            return (IList<MKtSpectSubtype>)ExecuteQuery(query);
        }
       
        /// <summary>
        /// 选择最大的专题类别编号（编号1——4为固定使用，不在选择范围内）
        /// </summary>
        /// <returns></returns>
        public static int SelectMaxNO()
        {
            string hql = @"select max(subtype.SubjectType) from MKtSpectSubtype subtype where subtype.SubjectType>4 " ;
            ScalarQuery query = new ScalarQuery(typeof(MKtSpectSubtype), hql);
            return Convert.ToInt32(ExecuteQuery(query));
        }
        /// <summary>
        /// 删除按排序号递增排序的第start+1条到end条记录
        /// </summary>
        /// <param name="num"></param>
        public static void DeleteByOrder(int start,int end,int type)
        {
            using(TransactionScope tran =new TransactionScope())
            {
                try
                {
                    string hql = @"from MKtSpectDetailitems item where item.MKtSpectSubtype.SubjectType="+type+" order by item.OrderNO,ID";
                    SimpleQuery query = new SimpleQuery(typeof(MKtSpectDetailitems), hql);
                    query.SetQueryRange(start, end - start);
                    MKtSpectDetailitems[] itemlist = (MKtSpectDetailitems[])ExecuteQuery(query);
                    for (int i = 0; i < itemlist.Length; i++)
                    {
                        itemlist[i].Delete();
                    }
                    tran.VoteCommit();
                }
                catch (System.Exception e)
                {
                    tran.VoteRollBack();
                }
            }
        }

        /// <summary>
        /// 返回第start条记录后的max条记录
        /// </summary>
        /// <param name="start"></param>
        /// <param name="max"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static IList<MKtSpectSubtype> FindALLSliced(int start, int max, string where)
        {
            string hql = @"from MKtSpectSubtype subtype where " + where;
            SimpleQuery query = new SimpleQuery(typeof(MKtSpectSubtype), hql);
            query.SetQueryRange(start, max);
            return (IList<MKtSpectSubtype>)ExecuteQuery(query);
        }
        /// <summary>
        /// 根据where条件返回符合条件记录数
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public static int GetCount(string where)
        {
            string hql = @"select count(*) from MKtSpectSubtype subtype where " + where;
            ScalarQuery query = new ScalarQuery(typeof(MKtSpectSubtype), hql);
            return Convert.ToInt32(ExecuteQuery(query));
        }
        [HasMany(typeof(MKtSpectDetailitems), ColumnKey = "SUBJECT_TYPE")]
        public IList<MKtSpectDetailitems> MKtSpectDetailitems
        {
            get
            {
                return _MKtSpectDetailitems;
            }
            set
            {
                _MKtSpectDetailitems = value;
            }
        }

        /// <summary>
        /// Gets or sets the SubjectType.(1：属于专题（默认）
        ////2：重要视角
        ////3：投资小教室
        ////4：投资渠道
        ////)
        /// </summary>
        /// <value>The SubjectType.</value>
        [PrimaryKey(PrimaryKeyType.Assigned,"SUBJECT_TYPE")]
        public int SubjectType
        {
            get
            {
                return _SubjectType;
            }
            set
            {
                _SubjectType = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the ID.()
        /// </summary>
        /// <value>The ID.</value>
        [Property("ID",Insert=false,Update=false)]
        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the SubjectTypename.()
        /// </summary>
        /// <value>The SubjectTypename.</value>
        [Property("SUBJECT_TYPENAME")]
        public string SubjectTypename
        {
            get
            {
                return _SubjectTypename;
            }
            set
            {
                _SubjectTypename = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the TitleDisplay.()
        /// </summary>
        /// <value>The TitleDisplay.</value>
        [Property("TITLE_DISPLAY")]
        public string TitleDisplay
        {
            get
            {
                return _TitleDisplay;
            }
            set
            {
                _TitleDisplay = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the AddresDsplay.()
        /// </summary>
        /// <value>The AddresDsplay.</value>
        [Property("ADDRES_DSPLAY")]
        public string AddresDsplay
        {
            get
            {
                return _AddresDsplay;
            }
            set
            {
                _AddresDsplay = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the DisplayNum.()
        /// </summary>
        /// <value>The DisplayNum.</value>
        [Property("DISPLAY_NUM")]
        public int? DisplayNum
        {
            get
            {
                return _DisplayNum;
            }
            set
            {
                _DisplayNum = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the IsValidate.()
        /// </summary>
        /// <value>The IsValidate.</value>
        [Property("IS_VALIDATE")]
        public int? IsValidate
        {
            get
            {
                return _IsValidate;
            }
            set
            {
                _IsValidate = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the InputTime.()
        /// </summary>
        /// <value>The InputTime.</value>
        [Property("INPUT_TIME")]
        public DateTime? InputTime
        {
            get
            {
                return _InputTime;
            }
            set
            {
                _InputTime = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the ModifyTime.()
        /// </summary>
        /// <value>The ModifyTime.</value>
        [Property("MODIFY_TIME")]
        public DateTime? ModifyTime
        {
            get
            {
                return _ModifyTime;
            }
            set
            {
                _ModifyTime = value;
            }
        }
        
        
        /// <summary>
        /// Gets or sets the DeleteTime.()
        /// </summary>
        /// <value>The DeleteTime.</value>
        [Property("DELETE_TIME")]
        public DateTime? DeleteTime
        {
            get
            {
                return _DeleteTime;
            }
            set
            {
                _DeleteTime = value;
            }
        }
        
        

        private int _SubjectType;
        
        private IList<MKtSpectDetailitems> _MKtSpectDetailitems;
        private int _ID;
        
        private string _SubjectTypename;
        
        private string _TitleDisplay;
        
        private string _AddresDsplay;
        
        private int? _DisplayNum;
        
        private int? _IsValidate;
        
        private DateTime? _InputTime;
        
        private DateTime? _ModifyTime;
        
        private DateTime? _DeleteTime;
        

    }
}
